<!DOCTYPE html>
<html lang="zh-cmn-Hans">

	<head>
		<meta http-equiv="Expires" content="0" />
		<meta http-equiv="Pragma" content="no-cache" />
		<meta http-equiv="Cache-control" content="no-cache" />
		<meta http-equiv="Cache" content="no-cache" />
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
		<title>QData</title>
		<link rel="stylesheet" href="../layui/css/layui.css" />
		<link rel="stylesheet" type="text/css" href="../jexcel-master/dist/css/jquery.jexcel.css" />
	</head>

	<body>
		<fieldset class="layui-elem-field site-demo-button">
			<legend><big><b>QC巡检程序</b></big></legend>
			<div class="layui-fluid layui-form layui-form-pane">
				<div class="layui-form-item">
					<!-- <div class="layui-row"> -->
						<!-- <label class="layui-form-label">显示工序:</label> -->
						<!-- <input type="radio" name="gx[]" value="固晶" title="固晶" lay-filter="gx[]"> -->
						<!-- <input type="radio" name="gx[]" value="焊线" title="焊线" lay-filter="gx[]"> -->
						<!-- <input type="radio" name="gx[]" value="点胶" title="点胶" lay-filter="gx[]"> -->
						<!-- <input type="radio" name="gx[]" value="切割" title="切割" lay-filter="gx[]"> -->
						<!-- <input type="radio" name="gx[]" value="分光" title="分光" lay-filter="gx[]"> -->
						<!-- <input type="radio" name="gx[]" value="编带" title="编带" lay-filter="gx[]"> -->
					<!-- </div> -->
					<label class="layui-form-label">扫码框:</label>
					<div class="layui-input-block">
						<input class="layui-input" placeholder="请按顺序扫入二维码。" id="qr" type="text" />
					</div>
					<div class="layui-row">
						<label class="layui-form-label">检规:</label>
						<div class="layui-input-block">
							<input type="checkbox" name="Fruit[]" title="固晶" lay-skin="primary" lay-filter="primaryTest[]" value="固晶">
							<input type="checkbox" name="Fruit[]" title="焊线" lay-skin="primary" lay-filter="primaryTest[]" value="焊线">
							<input type="checkbox" name="Fruit[]" title="点胶" lay-skin="primary" lay-filter="primaryTest[]" value="点胶">
							<input type="checkbox" name="Fruit[]" title="下料" lay-skin="primary" lay-filter="primaryTest[]" value="下料">
							<input type="checkbox" name="Fruit[]" title="切割" lay-skin="primary" lay-filter="primaryTest[]" value="切割">
							<input type="checkbox" name="Fruit[]" title="分光" lay-skin="primary" lay-filter="primaryTest[]" value="分光">
							<input type="checkbox" name="Fruit[]" title="编带" lay-skin="primary" lay-filter="primaryTest[]" value="编带">
						</div>
					</div>
					<!-- <div class="layui-row"> -->
						<!-- <label class="layui-form-label">轮询时间(s):</label> -->
						<!-- <div class="layui-input-block"> -->
							<!-- <input class="layui-input" placeholder="设置轮询的时间,单位:秒(修改后立即生效)" id="LXSJ" type="text" /> -->
						<!-- </div> -->
					<!-- </div> -->
				</div>
			</div>
		</fieldset>
		<fieldset class="layui-elem-field site-demo-button">
			<legend><big><b>这里是当前信息组合</b></big></legend>
			<div id="DQ" class="layui-fluid layui-form layui-form-pane">
				<div class="layui-row">
					<div class="layui-col-sm6">
						<label class="layui-form-label">员工工号:</label>
						<div class="layui-input-block">
							<input class="layui-input" readonly=true placeholder="这里是员工工号(请扫入工号)" id="YGGH" type="text" />
						</div>
					</div>
					<div class="layui-col-sm6">
						<label class="layui-form-label">员工姓名:</label>
						<div class="layui-input-block">
							<input class="layui-input" readonly=true placeholder="这里是员工姓名(根据工号自动产生)" id="YGXM" type="text" />
						</div>
					</div>
				</div>
				<div class="layui-row">
					<div class="layui-col-sm6">
						<label class="layui-form-label">当前机台:</label>
						<div class="layui-input-block">
							<input class="layui-input" readonly=true placeholder="这里是当前机台(请扫入机台号)" id="DQJT" type="text" />
						</div>
					</div>
					<div class="layui-col-sm6">
						<label class="layui-form-label">当前工单号:</label>
						<div class="layui-input-block">
							<input class="layui-input" readonly=true placeholder="这里是当前工单号(请扫入工单)" id="GDH" type="text" />
						</div>
					</div>
				</div>
				<div class="layui-row">
					<div class="layui-col-sm6">
						<label class="layui-form-label">检验数量:</label>
						<div class="layui-input-block">
							<input class="layui-input" placeholder="检验数量,自动产生(可修改)" id="JYQTY" type="text" />
						</div>
					</div>
					<div class="layui-col-sm6">
						<label class="layui-form-label">不良率:</label>
						<div class="layui-input-block">
							<input class="layui-input" readonly=true placeholder="不良率(自动产生)" id="BLL" type="text" />
						</div>
					</div>
				</div>
				<!-- 这里是JExcel的表格位置 -->
				<div id="jyxm"></div>
				<div class="layui-row">
					<label class="layui-form-label">判定结果:</label>
					<input type="radio" name="jyjg" value="OK" title="OK判定通过" lay-filter="jyjg">
					<input type="radio" name="jyjg" value="NG" title="NG检验不通过" lay-filter="jyjg">
				</div>
				<div class="layui-row">
					<div class="layui-form-item layui-form-text layui-col-sm12">
						<label class="layui-form-label ">整单备注:</label>
						<div class="layui-input-block">
							<textarea placeholder="请填写备注" class="layui-textarea" id="BZSTR"></textarea>
						</div>
					</div>
				</div>
			</div>
			<div class="layui-row layui-btn-container">
				<div class="layui-col-sm4">
					<button class="layui-btn  layui-btn-normal" id="keep">保存</button>
				</div>
			</div>
		</fieldset>
		<fieldset class="layui-elem-field site-demo-button layui-btn-container">
			<legend><big><b>检验规范列表（自动排序）</b></big></legend>
			<div class="layui-fluid layui-form layui-form-pane">
				<table class="layui-hide" id="Demo"></table>
			</div>
		</fieldset>
		<!-- 加载jQuery文件 -->
		<script src="../js/jquery.min.js"></script>
		<!-- 加载类ui文件 -->
		<script src="../layui/layui.js"></script>
		<!-- 加载JExcel文件 -->
		<script src="../jexcel-master/dist/js/jquery.jexcel.js"></script>
		<!-- 写脚本 -->
		<script>
			var Hhtml = ""; //画窗口的画面,把程序写到Layer里面
			var Dhtml = ""; //画窗口的画面,把程序写到Layer里面
			var ZiDian2 = {} //字典2
			var data2 = {} //JExcel数据
			var JYJG = ""; //检验结果,全局变量,用于发后台存储
			var HH = [] //HH作为全局传递Hdata的数组
			var DD = [] //DD作为全局传递Ddata的数组
			var XM = ""; //姓名暴露给全局,然后可以同步ajax得出值
			var Dlist = []; //表身数据,喂给LayTable数据

			var DuXie1 = [{
				type: 'text',
				readOnly: true
			}, {
				type: 'text',
				readOnly: true
			}, {
				type: 'text',
				readOnly: true
			}, {
				type: 'number',
				wordWrap: true
			}, {
				type: 'text',
				readOnly: true
			}];
			var LieMing1 = [
				'检验代码', '检验项目', 'AQL', '不良数量(K)', '不良率'
			];
			var LieKuan1 = [
				100, 200, 150, 150, 100
			];
			$(document).ready(function() {
				//这里是画面准备完毕后执行的程式
				layui.use(['layer', 'form', 'element', 'table'], function() { //载入layui模块:layer弹出层,form表单,element元素组件做分页或其他
					//layer.msg('初始化正常!');
					var layer = layui.layer,
						form = layui.form,
						element = layui.element,
						table = layui.table;

					if(localStorage.yggh) { //如果HTML5WEB存储存在,就查询,否则就弹窗提示
						$("#YGGH").val(localStorage.yggh);
						var sss = getxm(localStorage.yggh);
						$("#YGXM").val(sss);
						layer.msg(sss + '，欢迎回来!');
						localStorage.ygxm = sss;
					} else {
						layer.msg("请先登录");
					}

					//监听指定复选框
					form.on('checkbox(primaryTest[])', function(data) {
						var bm_num;
						var names = document.getElementsByName("Fruit[]");
						for(var k = 0; k < names.length; k++) {
							if(names[k].checked) {
								if(bm_num) {
									bm_num += "," + names[k].value;
								} else {
									bm_num = names[k].value;
								}
							}
						}
						localStorage.gx = bm_num;
						console.log(localStorage.gx);
						location.reload(); //如果是检规就刷新好了, 反正lay-table又弄不懂
					});

					//监听指定单选框(干掉了)
					//form.on('radio(gx[])', function(data) {
						//console.log(data.elem);// 得到radio原始DOM对象
						//console.log(data.value); // 被点击的radio的value值
						//localStorage.jygx = data.value;
						//location.reload(); //变更了工序后就刷新整个画面
					//});

					//监听指定单选框
					form.on('radio(jyjg)', function(data) {
						//console.log(data.elem); //得到radio原始DOM对象
						console.log(data.value); //被点击的radio的value值
						JYJG = data.value;
					});
				});
				$("#qr").select(); //画面准备完毕后就选择扫码输入框

				var bm_num;
				var gx = {}
				var str = localStorage.gx;
				var n;
				if(str) {
					n = str.split(",");
				}

				for(var k in n) {
					gx[n[k]] = n[k];
				}

				var names = document.getElementsByName("Fruit[]");
				for(var k in names) {
					if(gx[names[k].value]) {
						names[k].checked = true;
						//console.log(names[k].checked);
					}
				}

				names = document.getElementsByName("gx[]");
				for(var k in names) {
					if(names[k].value == localStorage.jygx) {
						names[k].checked = true;
						//console.log(names[k].checked);
					}
				}

				$('#jyxm').jexcel({
					onafterchange: BLLv, //值改变时就调用过程
					allowDeleteColumn: false, //不允许删除行
					rowDrag: false, //不允许拖动
					allowManualInsertRow: false, //禁止用户插入一个新列(只能程序增加)
					allowManualInsertColumn: false, //不允许新增列
					columnSorting: false, //不允许排序
					columns: DuXie1,
					colHeaders: LieMing1,
					colWidths: LieKuan1
				});
			});

			$("#qr").click(function() { //这里是扫描输入框的回车按下后执行的程式
				$("#qr").select();
			});

			$("#qr").keypress(function(e) {
				if(e.keyCode === 13) { //如果按下的键值是回车键13就执行以下语句
					$("#qr").select(); //扫描输入框选择
					var QRC = $("#qr").val(); //定义局部变量QRC=扫描输入框的值

					var GDHS = QRC.replace(/^TA/,""); //TAAN1891701-X T3C30821A-R4101A 10 001
					var pattgdh = /^[A-Z]{2}[A-Z\d]{7}(\-[A-Z\d])?(?= T)/; //得到工单号
					var pattxh = /\d{3}$/; //得到序号
					var patt3 = /[A-Z]{1,2}[A-Z\d]{7}(?:\-[A-Z\d])?(?= T)/; //AK187029C-X分光标签工单号
					var patt4 = /BP[A-Z]\d{4}[A-Z\d]*\-?[A-Z\d]*(?= [A-Z]{1,2})/; //BPI31798706019-1分光标签序号(用作唯一码)

					if(pattgdh.test(GDHS)) {
						$("#GDH").val(pattgdh.exec(GDHS)[0] + '-' + pattxh.exec(GDHS)[0]); //测试工单号
						ZiDian2 = {}
					} else if(patt3.test(QRC)) {
						$("#GDH").val(patt3.exec(QRC)[0] + "-" + patt4.exec(QRC)[0]); //分光标签工单号唯一
						ZiDian2 = {}
					}

					if(/^EM\d{5}$/.test(QRC)) {
						localStorage.yggh = QRC;
						location.reload(); //如果是工号,还要清除字典内容,重新来过...所以干脆刷新一下好了,下面是语句
					}

					if(/BP[A-Z]\d{4}$|BHG\d{4}$/.test(QRC)) {
						$("#DQJT").val(/BP[A-Z]\d{4}$|BHG\d{4}$/.exec(QRC)[0]); //测试机台号
						return;
					}

					//75 78 3 3.1 5GA H75GAAG 80 90 150 BPI31898929012-2 AD1892545 T2840811E-R4206A 4
					var pattlh = /T[A-Z\d]{8}(?:\-[A-Z\d]*?)?(?= \d+ \d{3})|A[A-Z\d]{11}(?:\-[A-Z\d]*?)?(?= \d+ \d{3})/; //测试产品型号
					var pattlh2 = /T[A-Z\d]{8}(?:\-[A-Z\d]*?)?(?= \d+)/; //测试分光编码产品型号
					var wxdm = "";
					if(pattlh.test(GDHS)) {
						wxdm = pattlh.exec(GDHS)[0].substr(1,2); // T3C30821A-R4101A 外形代码,20,34,3C,5C...
						Dlist = [];
					} else {
						wxdm= pattlh2.exec(GDHS)[0].substr(1,2); // T3C30821A-R4101A 外形代码,20,34,3C,5C...
						Dlist = [];
					}
					if(/^EM\d{5}$/.test($("#YGGH").val()) && /^[A-Z]{3}\d{4}$/.test($("#DQJT").val()) && (/^[A-Z]{2}[A-Z\d]{7}(\-[A-Z\d])?\-\d{3}$/.test($("#GDH").val()) || /^[A-Z]{2}[A-Z\d]{7}(\-[A-Z\d])?\-BP[A-Z]\d{4}[A-Z\d]*\-?[A-Z\d]*$/.test($("#GDH").val()))) {
						//全通过了，做成重刷，不然没办法，只能整个界面刷新了，所以要加功能
						data2 = {}
						$('#jyxm').jexcel('setData', [
							[]
						]);
						//当员工工号+机台号+工单号一致通过验证,就调用阿贾克斯去获取数据
						$.ajax({
							type: 'POST',
							url: "http://172.31.3.27:84/index.php?s=/scancode/q_data/jybz",
							//url: "http://127.0.0.1:85/index.php?s=/scancode/q_data/jybz",
							data: "qrc=" + localStorage.yggh + "&gxx=" + localStorage.gx + "&wxdm=" + wxdm,
							dataType: "json",
							success: function(sss) {
								//console.log(sss);
								$("#JYQTY").val(sss.JYQTY); //填写检验数量
								//以下开始在检验规范列表里面填入对应的表格和按钮
								for (var k=0; k<sss.JYBZ.length; k++) {
									var d = sss['JYBZ'][k]['JYXMDM'];
									var jyxm = sss['JYBZ'][k]['JYMXSM'];
									var zb = sss['JYBZ'][k]['GongXu'];
									var jybz = sss['JYBZ'][k]['JYBZ'];
									var jyfs = sss['JYBZ'][k]['JYFS'];
									var jylb = sss['JYBZ'][k]['JYLBMC'];
									var jysz = sss['JYBZ'][k]['JYSZ'];
									var aql = sss['JYBZ'][k]['AQL'];
									ZiDian2[d] = [d, jyxm, jybz, jyfs, jylb, jysz, aql];
									//凑表身
									Dlist.push(
										{
											 'DM': "<button id='" + d + "' class='layui-btn layui-btn-xs layui-btn-normal' data-d='" + d + "' onclick='showJY(this)'>" + d + "</button>"
											,'XM': jyxm
											,'GX': zb
											,'BZ': jybz
											,'FS': jyfs
											,'LB': jylb
											,'SZ': jysz
											,'AQL': aql
										}
									)
								}
								layui.use('table', function(){
									var table = layui.table;
									//展示已知数据
									table.render({
										elem: '#Demo'
										,cols: [[ //标题栏
											 {field: 'DM', title: '代码', minWidth: 80}
											,{field: 'XM', title: '项目', minWidth: 100}
											,{field: 'GX', title: '工序', minWidth: 80}
											,{field: 'BZ', title: '标准', minWidth: 250}
											,{field: 'FS', title: '方式', minWidth: 150}
											,{field: 'LB', title: '类别', minWidth: 80}
											,{field: 'SZ', title: '水准', minWidth: 100}
											,{field: 'AQL', title: 'AQL', minWidth: 80}
										]]
										,data: Dlist
										//,skin: 'line' //表格风格
										//,even: true
										//,page: true //是否显示分页
										//,limits: [5, 7, 10]
										//,limit: 5 //每页默认显示的数量
									});
								});

							}
						});
					} else {
						layer.msg("<p align='left'>录入顺序请按：<br/>&nbsp;&nbsp;&nbsp;&nbsp;1.员工工号(只需要进入的时候输一次)<br/>&nbsp;&nbsp;&nbsp;&nbsp;2.机台号<br/>&nbsp;&nbsp;&nbsp;&nbsp;3.工单号<br/>当3者都有值且正确时，才会生成检验规范列表</p>");
					}
				}
			});

			function showJY(data) {
				var d1 = data.getAttribute("data-d");
				//console.log(d1);
				data.disabled = true; //开始时间按钮被禁用
				data.className = 'layui-btn layui-btn-xs layui-btn-normal layui-btn-disabled'; //添加禁用渲染
				//layer.msg("Layer的按钮被点击!");
				data2[d1] = [d1, ZiDian2[d1][0], ZiDian2[d1][1], ZiDian2[d1][6]];
				var result = Object.keys(data2).map(function(key) {
					return [data2[key][1], data2[key][2], data2[key][3]];
				});
				$('#jyxm').jexcel('setData', result);
			}

			$("#keep").click(function() {
				HH = [];
				DD = [];
				var CNT = [];
				var Ddata = $('#jyxm').jexcel('getData');
				if(JYJG && $("#JYQTY").val()) {
					//console.log(JYJG);
					if(JYJG == 'NG' && Ddata[0][3] == '' && Ddata.length < 1) {
						layer.tips("<span style='color:red'><big><b>未保存！NG时，请确认【不良数量】是否有输入、不良数是否有输入、是否有选择不良数。</b></big></span>", this, {
							tips: [2, '#fff']
						});
					//} else if(JYJG == 'OK' && Ddata[0][0] != '') {
						//layer.tips("<span style='color:red'><big><b>未保存！OK时，请不要点检验项目，或请切换/刷新单据，重新记录。</b></big></span>", this, {
							//tips: [2, '#fff']
						//});
					} else {
						for(var k in Ddata) {
							DD.push({
								"BLXMID": Ddata[k][0],
								"BLQTY": Ddata[k][3]
							});
						}
						HH.push({
							'JTH': $("#DQJT").val(),
							'GDH': $("#GDH").val(),
							'YGGH': localStorage.yggh,
							'ChouYangQTY':  $("#JYQTY").val(),
							'JG': JYJG,
							'BZ': $("#BZSTR").val()
						});
						//layer.msg('假装自己被传到后端保存了');
						console.log({
							'HH': HH,
							'DD': DD
						});

						$.ajax({
							type: 'POST',
							url: "http://172.31.3.27:84/index.php?s=/scancode/q_data/keep",
							//url: "http://127.0.0.1:85/index.php?s=/scancode/q_data/keep",
							data: "hdjson=" + JSON.stringify({
								'HH': HH,
								'DD': DD
							}),
							dataType: "json",
							success: function(sss) {
								console.log(sss);
								if(sss['msg']) {
									layer.msg(sss['msg']);
									if(sss['msg'] == '保存成功') {
										data2 = {}
										$('#jyxm').jexcel('setData', [
											[]
										]);
										$('#BZSTR').val("");
									}
								}
								
								Dlist = [];
								layui.use('table', function(){
									var table = layui.table;
									//展示已知数据
									table.render({
										elem: '#Demo'
										,cols: [[ //标题栏
											 {field: 'DM', title: '代码', minWidth: 80}
											,{field: 'XM', title: '项目', minWidth: 100}
											,{field: 'GX', title: '工序', minWidth: 80}
											,{field: 'BZ', title: '标准', minWidth: 250}
											,{field: 'FS', title: '方式', minWidth: 150}
											,{field: 'LB', title: '类别', minWidth: 80}
											,{field: 'SZ', title: '水准', minWidth: 100}
											,{field: 'AQL', title: 'AQL', minWidth: 80}
										]]
										,data: Dlist
										//,skin: 'line' //表格风格
										//,even: true
										//,page: true //是否显示分页
										//,limits: [5, 7, 10]
										//,limit: 5 //每页默认显示的数量
									});
								});
							}

						});
					}
				} else {
					layer.tips("<span style='color:red'><big><b>未保存！请检查【送检代码】和【检验结果】是否有选择，【抽检数量】是否有填写！</b></big></span>", this, {
						tips: [2, '#fff']
					});
				}

			});

			var BLLv = function() { //自动计算不良率
				var JYQTY = $('#JYQTY').val(); //巡检数量,自动产生
				var Ddata = $('#jyxm').jexcel('getData');
				if(JYQTY) {
					var sum = 0;
					for(var k in Ddata) {
						sum += Ddata[k][3] * 1;
						console.log(sum);
						Ddata[k][4] = ((Ddata[k][3] / JYQTY * 100).toFixed(2) + '%');
					}
					$('#BLL').val((sum / JYQTY * 100).toFixed(2) + '%');

					$('#jyxm').jexcel('setData', Ddata);
					console.log(Ddata);
				} else {
					layer.msg("请输入不良总数");
				}
			}

			function getxm(gh) { //得到姓名的公式
				XM = "";
				$.ajax({
					type: 'POST',
					url: "http://172.31.3.27:84/index.php?s=/scancode/q_data/xm",
					//url: "http://127.0.0.1:85/index.php?s=/scancode/q_data/xm",
					async: false,
					data: "qrc=" + gh,
					dataType: "text",
					success: function(sss) {
						console.log(sss);
						XM = sss;
					}
				});
				return XM;
			}
		</script>
	</body>

</html>